package weaver.interfaces.cyjt.duss.utils;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.docs.docs.util.DesUtils;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author dusongsong
 * @date 2024年06月11日 16:05
 */
public class ArchiveUtil extends BaseBean {
    /**
     * 发送请求
     *
     * @param params
     * @return
     */
    public JSONObject sendPostOKHttpRequest(String params) {
        JSONObject resultJson = new JSONObject();
        String message = "";
        String data = "";
        String baseUrl = Util.null2String(getPropValue("archive", "url"));
        String apiUrl = baseUrl + "/api/archive/base/recieveData";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, params);
            Request request = new Request.Builder()
                    .url(apiUrl)
                    .method("POST", body)
                    .build();
            Response response = client.newCall(request).execute();
            String result = Util.null2String(response.body().string());
            if (!"".equals(result)) {
                data = result;
                message = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            writeLog(">>>>>sendOKHttpRequest>>>>>Exception=" + e.getMessage(), e);
        }
        resultJson.put("data", data);
        resultJson.put("message", message);
        return resultJson;
    }

    /**
     * 根据文档ID获取附件信息
     */
    public Map<String, Object> getImagefileInfo(String docid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select t1.imagefileid,t1.imagefilename,t1.imagefiletype,t1.filesize,t2.operatedate,t2.operatetime \n" +
                "from imagefile t1 join docimagefile t2 on t1.imagefileid=t2.imagefileid where t2.docid=?", docid);
        rs.next();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("imagefileid", Util.null2String(rs.getString("imagefileid")));
        resultMap.put("operatedate", Util.null2String(rs.getString("operatedate")));
        resultMap.put("operatetime", Util.null2String(rs.getString("operatetime")));
        resultMap.put("imagefilename", Util.null2String(rs.getString("imagefilename")));
        resultMap.put("imagefiletype", Util.null2String(rs.getString("imagefiletype")));
        resultMap.put("filesize", Util.null2String(rs.getString("filesize")));
        return resultMap;
    }

    /**
     * 生成token
     */
    public String getTokenKey(String imagefileid) {
        String ddcode = 1 + "_" + imagefileid;
        DesUtils des = null;
        try {
            des = new DesUtils();
            ddcode = des.encrypt(ddcode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ddcode;
    }

    /**
     * 获取人员ID
     */
    public String getUserId(String userid) {
        String id = "";
        RecordSet rs = new RecordSet();
        rs.executeQuery("select loginid from hrmresource where id='" + userid + "'");
        rs.next();
        String loginid = Util.null2String(rs.getString("loginid"));
        if (!loginid.isEmpty()) {
            RecordSetDataSource rsd = new RecordSetDataSource("archive");
            rsd.execute("select id from hrmresource where loginid='" + loginid + "'");
            rsd.next();
            id = Util.null2String(rsd.getString("id"));
        }
        return id;
    }
    /**
     * 获取部门ID
     */
    public String getDeptId(String departmentid) {
        String id = "";
        RecordSet rs = new RecordSet();
        rs.executeQuery("select departmentcode from hrmdepartment where id='" + departmentid + "'");
        rs.next();
        String departmentcode = Util.null2String(rs.getString("departmentcode"));
        if (!departmentcode.isEmpty()) {
            RecordSetDataSource rsd = new RecordSetDataSource("archive");
            rsd.execute("select id from hrmdepartment where departmentcode='" + departmentcode + "'");
            rsd.next();
            id = Util.null2String(rsd.getString("id"));
        }
        return id;
    }

}
